#include "LED.h"
#include "PCF8563.h"

LED led;

void LED::time(u8 time_hour,u8 time_min)
{
  u8 i;
  u8 dat[4];
  u8 ram_num=0;
  dat[0]=time_hour/10;
  dat[1]=time_hour%10;
  dat[2]=time_min/10;
  dat[3]=time_min%10;
  if(last_time_data[0]!=dat[0])
  {
    last_time_flg[0] = 1;
  }
  if(last_time_data[1]!=dat[1])
  {
    last_time_flg[1] = 1;
  }
  if(last_time_data[2]!=dat[2])
  {
    last_time_flg[2] = 1;
  }
  if(last_time_data[3]!=dat[3])
  {
    last_time_flg[3] = 1;
  }
  if(last_time_flg[0])
  {
    
    for(i=0;i<5;i++)
    {
      led_dat_gram[ram_num] = 0;
      if(last_time_data[0] != 255)
        led_dat_gram[ram_num] |= F5x8[last_time_data[0]][i]>>time_change_cont;
      led_dat_gram[ram_num++] |= F5x8[dat[0]][i]<<(8-time_change_cont);
    } 
  }
  if(last_time_flg[1])
  {
    ram_num = 5;
    for(i=0;i<5;i++)
    {
      led_dat_gram[ram_num] = 0;
      if(last_time_data[1] != 255)
        led_dat_gram[ram_num] |= F5x8[last_time_data[1]][i]>>time_change_cont;
      led_dat_gram[ram_num++] |= F5x8[dat[1]][i]<<(8-time_change_cont);
    } 
  }

  if(last_time_flg[2])
  {
    ram_num = 12;
    for(i=0;i<5;i++)
    {
      led_dat_gram[ram_num] = 0;
      if(last_time_data[2] != 255)
        led_dat_gram[ram_num] |= F5x8[last_time_data[2]][i]>>time_change_cont;
      led_dat_gram[ram_num++] |= F5x8[dat[2]][i]<<(8-time_change_cont);
    } 
  }
  if(last_time_flg[3])
  {
    ram_num = 17;
    for(i=0;i<5;i++)
    {
      led_dat_gram[ram_num] = 0;
      if(last_time_data[3] != 255)
        led_dat_gram[ram_num] |= F5x8[last_time_data[3]][i]>>time_change_cont;
      led_dat_gram[ram_num++] |= F5x8[dat[3]][i]<<(8-time_change_cont);
    } 
  }
  if(last_time_flg[0]||last_time_flg[1]||last_time_flg[2]||last_time_flg[3])
  {
    time_change_cont++;
    if(time_change_cont==9)
    {
      last_time_data[0] = dat[0];
      last_time_data[1] = dat[1];
      last_time_data[2] = dat[2];
      last_time_data[3] = dat[3];
      last_time_flg[0] = 0;
      last_time_flg[1] = 0;
      last_time_flg[2] = 0;
      last_time_flg[3] = 0;
      time_change_cont = 1;
    }
  }
}

void LED::second(u8 dat)
{
  u8 i;
  u8 tmp;
  if(last_second!=dat)
  {
    second_buf=dat/7.5+1;
    if(second_buf>8)
    {
      second_buf=8;
    }
    change_flg=1;
    last_second=dat;
  }
  if(change_flg)
  {
    led_dat_gram[10] = 0;
    led_dat_gram[11] = 0;
    for(i=0;i<second_num;i++)
    {
      tmp=0x80>>i;
      led_dat_gram[10]|=tmp;
      led_dat_gram[11]|=tmp;
    }
    if(stay_flg<2)
    {
      if(second_mode)
        second_num++;
      else
        second_num--;
    }
    else
    {
      stay_flg --;
    }
    if(second_num==second_buf&&!stay_flg)
    {
      second_mode=!second_mode;
      stay_flg = 3;
    }
    if(second_num==-1)
    {
      second_num=0;
      second_mode=!second_mode;
      change_flg = 0;
      stay_flg = 0;
    }
  }
}

void LED::battery(u8 bat_cap)
{
  char i;
  led_dat_gram[22]=0;
  if(bat_show_flg)
  {
    for(i=0;i<bat_cap;i++)
    {
      led_dat_gram[22]|=0x80>>i;
    }
  }
}

void LED::color(unsigned short mode)
{
  char i,l=0;
  u16 flg;
  u8 dat[2];
  

  led_light_gram=mode>>1;
  switch (color_mode)
  {
    case 0:
      for(i=0;i<11;i++)
      {
        flg = mode-mode*i/22;
        if(flg>255)
        {
          dat[0]=255;
        }
        else
        {
          dat[0]=flg;
        }
        dat[1] = mode-dat[0];
        led_color_gram[l++]=dat[1];
        led_color_gram[l++]=dat[0];
      }
      l=43;
      for(i=0;i<11;i++)
      {
        flg = mode-mode*i/22;
        if(flg>255)
        {
          dat[0]=255;
        }
        else
        {
          dat[0]=flg;
        }
        dat[1] = mode-dat[0];
        led_color_gram[l--]=dat[1];
        led_color_gram[l--]=dat[0];
      }
      led_color_gram[44]=led_color_gram[42];
      led_color_gram[45]=led_color_gram[43];
    break;
    case 1:
      for(i=0;i<11;i++)
      {
        flg = mode-mode*i/22;
        if(flg>255)
        {
          dat[0]=255;
        }
        else
        {
          dat[0]=flg;
        }
        dat[1] = mode-dat[0];
        led_color_gram[l++]=dat[0];
        led_color_gram[l++]=dat[1];
      }
      l=43;
      for(i=0;i<11;i++)
      {
        flg = mode-mode*i/22;
        if(flg>255)
        {
          dat[0]=255;
        }
        else
        {
          dat[0]=flg;
        }
        dat[1] = mode-dat[0];
        led_color_gram[l--]=dat[0];
        led_color_gram[l--]=dat[1];
      }
      led_color_gram[44]=led_color_gram[42];
      led_color_gram[45]=led_color_gram[43];
    break;
    case 2:
      if(mode==1&&color_mix>5)
        flg=1;
      else
        flg = mode*color_mix*410/4096;
      if(flg>255)
      {
        dat[0]=255;
      }
      else
      {
        dat[0]=flg;
      }
      if(mode-flg>255)
        dat[0]=mode-255;
      dat[1]= mode-dat[0];
      for(i=0;i<23;i++)
      {
          led_color_gram[l++]=dat[0];
          led_color_gram[l++]=dat[1];
      }
    break;
  }
}

void LED::write()
{
  u8 i,j=0;
  Serial.write(uart_head,3);
  Serial.write(pcf.second);
  Serial.write(pcf.min);
  Serial.write(pcf.hour);
  // for(i=0;i<23;i++)
  // {
  //   Serial.write(led_dat_gram[i]);
  //   Serial.write(led_color_gram[j++]);
  //   Serial.write(led_color_gram[j++]);
  // }
  Serial.write(led_light_gram);
}

void LED::begin() 
{
  Serial.begin(115200);//UART_INIT
  pinMode(2,OUTPUT);
  digitalWrite(2,1);
}
void LED::end()
{
  Serial.end();
}

void LED::power(bool mode)
{
  digitalWrite(2,mode);
}
